const fs = require('fs');
const path = require('path');
const glob = require('glob');

function sortObject(obj) {
    if (typeof obj !== 'object' || obj === null) return obj;
    if (Array.isArray(obj)) return obj.map(sortObject);

    return Object.keys(obj)
        .sort()
        .reduce((sorted, key) => {
            sorted[key] = sortObject(obj[key]);
            return sorted;
        }, {});
}

// 排序所有 JSON 文件
const files = glob.sync('./src/locales/*.json');
files.forEach(file => {
    const content = fs.readFileSync(file, 'utf8');
    const data = JSON.parse(content);
    const sorted = sortObject(data);

    fs.writeFileSync(file, JSON.stringify(sorted, null, 2));
    console.log(`Sorted: ${file}`);
});